package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Application;
import com.sc.pojo.Relation;
import com.sc.pojo.User;
import com.sc.service.ApplicationService;
import com.sc.service.FriendsService;
import com.sc.service.RelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 陈远坤
 */
@Controller
@RequestMapping("/friends")
public class FriendsController {
    @Autowired
    FriendsService fs;
    @Autowired
    ApplicationService as;
    @Autowired
    RelationService rs;
    //查询陌生人
    @RequestMapping("/selectFriends")
    public String selectFriends(@RequestParam(defaultValue = "1")Integer pageNum, HttpSession session, Map m,String likes){
        User u = (User) session.getAttribute("theUser");
        PageInfo<Application> ap = as.selectappById(u.getId(),pageNum,4);
            m.put("ap",ap);
        if (likes==null){
            PageInfo<User> p =fs.selectFriends(pageNum,4,u) ;
            m.put("p",p);
            return "/friends/list";
        }else {
            PageInfo<User> p = fs.selectFriendsByLikes(pageNum,4,u,likes);
            m.put("p",p);
            m.put("likes",likes);
            return "forward:/friends/toList";
        }
    }

    //查询好友
    @RequestMapping("/selectAllfs")
    public String selectAllfs(HttpSession session,Map m,@RequestParam(defaultValue = "1") Integer pageNum,String likes){
        User u1 = (User)session.getAttribute("theUser");
        if (likes==null){
           PageInfo<User> p = fs.selectAllfs(pageNum,4,u1);
           m.put("p",p);
       }else {
           PageInfo<User> p = fs.selectAllfsByLikes(pageNum,4,u1,likes);
           m.put("p",p);
           m.put("likes",likes);
       }
        return "forward:/friends/toFriends";
    }
    //添加陌生人好友
    @RequestMapping("/acceptFr")
    public String acceptFr(HttpSession session,Integer sendid){
        User u1 = (User)session.getAttribute("theUser");
        Relation r = new Relation();
        r.setUser1id(u1.getId());
        r.setUser2id(sendid);
        Relation r2 = new Relation();
        r2.setUser2id(u1.getId());
        r2.setUser1id(sendid);
        r.setRelation(1);
        r2.setRelation(1);
        rs.acceptFr(r);
        rs.acceptFr(r2);
        as.deleteBySendId(sendid,u1.getId());
        return "redirect:/friends/selectAllfs";
    }
    //拉黑陌生人
    @RequestMapping("/lahei")
    public String lahei(Integer acceptid,HttpSession session){
        User u1 = (User)session.getAttribute("theUser");
        Relation r = new Relation();
        r.setUser1id(u1.getId());
        r.setUser2id(acceptid);
        r.setRelation(2);
        Relation r2 = new Relation();
        r2.setUser2id(u1.getId());
        r2.setUser1id(acceptid);
        r2.setRelation(2);
        rs.acceptFr(r);
        rs.acceptFr(r2);
        return "redirect:/friends/selectFriends";
    }
    //查询黑名单
    @RequestMapping("/selecthmd")
    public String selecthmd(HttpSession session,@RequestParam(defaultValue = "1")Integer pageNum,Map m,String likes){
        User u1 = (User)session.getAttribute("theUser");
       if (likes==null){
           PageInfo<User> p = fs.selectHmd(u1,pageNum,4);
           m.put("p",p);
       }else {
           PageInfo<User> p =fs.selectHmdByLikes(u1,pageNum,4,likes);
           m.put("p",p);
           m.put("likes",likes);
       }
        return "forward:/friends/toHmd";
    }
    //删除好友
    @RequestMapping("/deleteFriend")
    public String deleteFriend(Integer acceptid,HttpSession session){
        User u = (User)session.getAttribute("theUser");
        rs.deleteFr(acceptid,u.getId());
        return "forward:/friends/selectAllfs";
    }
    //删除多个好友
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] cks,HttpSession session){
        if (cks==null) return "forward:/friends/selectAllfs";
        System.out.println(Arrays.toString(cks));
        User u = (User)session.getAttribute("theUser");
        for (int i = 0; i < cks.length; i++) {
            rs.deleteFr(cks[i],u.getId());
        }
        return "forward:/friends/selectAllfs";
    }
    //拉黑好友
    @RequestMapping("/laheifr")
    public String laheifr(Integer userid,HttpSession session){
        User u = (User)session.getAttribute("theUser");
        rs.laHeiFr(u.getId(),userid);
        return "forward:/friends/selectAllfs";
    }
    //批量拉黑
    @RequestMapping("/laheiAll")
    public String laheiAll(Integer[] cks,HttpSession session){
        if (cks==null) return "forward:/friends/selectAllfs";
        User u = (User)session.getAttribute("theUser");
        for (int i = 0; i < cks.length; i++) {
            rs.laHeiFr(u.getId(),cks[i]);
        }
        return "forward:/friends/selectAllfs";
    }
    //移出黑名单
    @RequestMapping("/removehmd")
    public String removehmd(Integer userid,HttpSession session){
        User u = (User)session.getAttribute("theUser");
        rs.deleteFr(u.getId(),userid);
        return "redirect:/friends/selecthmd";
    }
    //批量移出黑名单
    @RequestMapping("/removehmdAll")
    public String removehmdAll(Integer[] ckss,HttpSession session){
        if (ckss==null) return "redirect:/friends/selecthmd";
        User u = (User)session.getAttribute("theUser");
        for (int i = 0; i < ckss.length; i++) {
            rs.deleteFr(u.getId(),ckss[i]);
        }
        return "redirect:/friends/selecthmd";
    }
    //去陌生人列表
    @RequestMapping("/toList")
    public String toList(){
        return "/friends/list";
    }
    //去好友列表
    @RequestMapping("/toFriends")
    public String toFriends(){
        return "/friends/myfriends";
    }
    //去黑名单
    @RequestMapping("/toHmd")
    public String toHmd(){
        return "/friends/heimingdan";
    }
}
